//
//  JXComtPopMenu.m
//  JiaoX
//
//  Created by yinqy on 15/2/28.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXComtPopMenu.h"


@interface JXComtPopMenu()

/**
 *  最底部的遮盖 ：屏蔽除菜单以外控件的事件
 */
@property (nonatomic, weak) UIButton *cover;

/**
 *  回复的Btn
 */
@property (nonatomic, strong) UIButton *replyBtn;

/**
 *  私聊的Btn
 */
@property (nonatomic, strong) UIButton *chatBtn;

@end
@implementation JXComtPopMenu


-(instancetype)initHaveArrow:(BOOL)haveArrow
{
    self = [super init];
    if (self) {
        /** 添加菜单内部的2个子控件 **/
        // 添加一个遮盖按钮
        UIButton *cover = [[UIButton alloc] init];
        cover.backgroundColor = [UIColor clearColor];//cover.alpha = 0.3;
        [cover addTarget:self action:@selector(coverClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:cover];
        self.cover = cover;
        
        // 添加带箭头的菜单图片
        UIImageView *container = [[UIImageView alloc] init];
        container.userInteractionEnabled = YES;
        [self addSubview:container];
        
        container.image = [UIImage imageNamed:@"bg_comt_menu"];
        if (haveArrow) {
            container.image = [UIImage imageNamed:@"bg_comt_menu_arrow"];
        }
        container.size = container.image.size;
        
        
        CGSize btnSize = CGSizeMake((container.width-30)/2, haveArrow?container.height-18:container.height-13);
        
        self.replyBtn = [self getBtnWithSize:btnSize Title:@"回复" icon:@"ic_pop_reply"];
        self.chatBtn = [self getBtnWithSize:btnSize Title:@"私信" icon:@"ic_pop_chat"];
        
        self.replyBtn.x = 10;
        self.replyBtn.y = 6.5;
        
        self.chatBtn.x = 20+self.replyBtn.width;
        self.chatBtn.y = self.replyBtn.y;
        
        [container addSubview:self.replyBtn];
        [container addSubview:self.chatBtn];
        
        [self.replyBtn addTarget:self action:@selector(replyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.chatBtn addTarget:self action:@selector(chatBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        self.container = container;
        
    }
    return self;
}

-(UIButton *)getBtnWithSize:(CGSize)size Title:(NSString *)title icon:(NSString *)icon{
    UIButton *btn = [[UIButton alloc]init];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:icon] forState:UIControlStateHighlighted];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
    [btn setBackgroundImage:[UIImage imageWithColor:JXThemeColor] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageWithColor:JXColor(222, 47, 128)] forState:UIControlStateHighlighted];
    [btn setSize:size];
    return btn;
}

-(void)showInPoint:(CGPoint)point{
    // 添加菜单整体到窗口身上
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.frame = window.bounds;
    [window addSubview:self];
    self.replyBtn.tag = self.tag;
    self.chatBtn.tag = self.tag;
    // 设置容器的frame
    self.container.x = point.x;
    self.container.y = point.y;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.cover.frame = self.bounds;
}

- (void)coverClick{
    [self dismiss];
}

-(void)dismiss{
    if ([self.delegate respondsToSelector:@selector(popMenuDidDismissed:)]) {
        [self.delegate popMenuDidDismissed:self];
    }
    [self removeFromSuperview];
}

-(void)replyBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(popMenuReplyBtnClick:)]) {
        [self.delegate popMenuReplyBtnClick:btn];
    }
    [self dismiss];
}

-(void)chatBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(popMenuChatBtnClick:)]) {
        [self.delegate popMenuChatBtnClick:btn];
    }
    [self dismiss];
}

@end
